using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Xeml.Document;
using Xeml.Document.Contracts;
using Xid.Controls.ParameterEditCtrls;

namespace Xid.Controls
{
    public partial class SsC_PartitionCtrl : UserControl
    {
        private Partition part;

        private SsC_ObservationControl creatorCtrl;

        public SsC_ObservationControl CreatorCtrl
        {
            get { return creatorCtrl; }
            set { creatorCtrl = value; }
        }

        private List<SsC_SampleCtrl> samples;

        public List<SsC_SampleCtrl> SampleCtrls
        {
            get { return samples; }
            set { samples = value; }
        }


        public Partition Partition
        {
            get { return part; }

        }

        IDocument _xemlDoc;
        public SsC_PartitionCtrl()
        {


            this.part = new Partition();
            this.samples = new List<SsC_SampleCtrl>();
            InitializeComponent();

            //comboBoxTerm.PreviewKeyDown += new PreviewKeyDownEventHandler(comboBoxTerm_PreviewKeyDown);
            textBoxStructTerm.PreviewKeyDown += new PreviewKeyDownEventHandler(textBox1_PreviewKeyDown);
        }

        void textBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                AddTermToList();
            }
        }

        private void AddTermToList()
        {

            IOntologyTerm sup = _currentTerms.Find(delegate(IOntologyTerm x) { return x.Prototype.Name == textBoxStructTerm.Text; });
            if (sup != null)
            {
                // IMaterialSupervisor sup = _currentTerms[textBoxTerm.Text];

                if (!part.Material.Contains(sup.TermId))
                {
                    Term m = sup.EmitDefaultObject(true);
                    part.Material.Add(m);
                    ListViewItem lvi = new ListViewItem(m.Name);
                    lvi.Tag = m;
                    lvi.ToolTipText = sup.Definition;

                    listViewStructure.Items.Add(lvi);
                    textBoxStructTerm.Text = "";
                }

            }
        }

        private void AddPosTermToList()
        {

            IOntologyTerm sup = _currentPosTerms.Find(delegate(IOntologyTerm x) { return x.Prototype.Name.ToLower().Trim() == textBoxPosTerm.Text.ToLower().Trim(); });
            if (sup != null)
            {
                if (!part.Positions.Contains(sup.TermId))
                {
                    Term m = sup.EmitDefaultObject(true);
                    VariableTermEditFrm editFrm = new VariableTermEditFrm();
                    editFrm.Setup(m, sup as IVariableOntologyTerm);
                    editFrm.ShowDialog(this);
                    part.Positions.Add(m);
                    ListViewItem lvi = new ListViewItem(m.Name);
                    lvi.Tag = m;
                    lvi.ToolTipText = sup.Definition;

                    listViewPositions.Items.Add(lvi);
                    textBoxPosTerm.Text = "";
                }
            }
        }



        public void Populate(IDocument doc)
        {
            _xemlDoc = doc;

            comboBoxOntologyAlias.SelectedValueChanged += new EventHandler(comboBoxOntologyAlias_SelectedValueChanged);
            comboBoxOntologyAlias.DataSource = _xemlDoc.DocumentResources.OrganismStructureOntologyHandler;
            comboBoxOntologyAlias.DisplayMember = "NameSpaceAlias";

            comboBoxPosAlias.SelectedValueChanged += new EventHandler(comboBoxPosAlias_SelectedValueChanged);
            comboBoxPosAlias.DataSource = _xemlDoc.DocumentResources.PositioningOntologyHandler;
            comboBoxPosAlias.DisplayMember = "NameSpaceAlias";
        }





        private List<IOntologyTerm> _currentTerms = new List<IOntologyTerm>();
        void comboBoxOntologyAlias_SelectedValueChanged(object sender, EventArgs e)
        {
            OntologyHandlerResource ohr = comboBoxOntologyAlias.SelectedValue as OntologyHandlerResource;
            if (ohr != null && ohr.ComponentState == ComponentLoadState.Loaded)
            {
                if (ohr.Handler.OntologyLoadState != OntologyLoadState.Loaded)
                {
                    ohr.LoadOntology();
                }
                if (ohr.Handler.OntologyLoadState == OntologyLoadState.Loaded)
                {

                    //comboBoxTerm.DataSource = (ohr.Handler as IOrganismStructureOntologyHandler).Supervisors;
                    //comboBoxTerm.DisplayMember = "Name";

                    _currentTerms.Clear();
                    textBoxStructTerm.AutoCompleteCustomSource.Clear();
                    foreach (IOntologyTerm isup in (ohr.Handler as IPlainOntologyHandler).Terms)
                    {
                        _currentTerms.Add(isup);
                    }

                    string[] terms = new string[_currentTerms.Count];
                    for (int i = 0; i < terms.Length; i++)
                    {
                        terms[i] = _currentTerms[i].Prototype.Name;
                    }

                    textBoxStructTerm.AutoCompleteCustomSource.AddRange(terms);
                }
                else
                {
                }
            }
            else
            {

            }
        }


        private List<IOntologyTerm> _currentPosTerms = new List<IOntologyTerm>();
        void comboBoxPosAlias_SelectedValueChanged(object sender, EventArgs e)
        {
            OntologyHandlerResource ohr = comboBoxPosAlias.SelectedValue as OntologyHandlerResource;
            if (ohr != null && ohr.ComponentState == ComponentLoadState.Loaded)
            {
                if (ohr.Handler.OntologyLoadState != OntologyLoadState.Loaded)
                {
                    ohr.LoadOntology();
                }
                if (ohr.Handler.OntologyLoadState == OntologyLoadState.Loaded)
                {
                    _currentPosTerms.Clear();
                    textBoxPosTerm.AutoCompleteCustomSource.Clear();
                    foreach (IOntologyTerm isup in (ohr.Handler as IVariableOntologyHandler).Terms)
                    {
                        _currentPosTerms.Add(isup);
                    }

                    string[] terms = new string[_currentPosTerms.Count];
                    for (int i = 0; i < terms.Length; i++)
                    {
                        terms[i] = _currentPosTerms[i].Prototype.Name;
                    }

                    textBoxPosTerm.AutoCompleteCustomSource.AddRange(terms);
                }
            }

        }


        //private void textBoxMaterial_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        //{
        //    if (e.KeyCode == Keys.Enter)
        //    {
        //        if (!part.Material.Contains(textBoxMaterial.Text))
        //        {

        //            listView1.Items.Add(textBoxMaterial.Text);
        //            Material ma = new Material(textBoxMaterial.Text);
        //            ma.Term = textBoxMaterial.Text;
        //            part.Material.Add(ma);
        //        }
        //        textBoxMaterial.Text = "";
        //    }
        //}



        public List<string> GetMaterialList()
        {
            List<string> ret = new List<string>();
            foreach (ListViewItem lvi in this.listViewStructure.Items)
            {
                ret.Add(lvi.Text);
            }
            return ret;
        }

        public event EventHandler<SampleControlEventArgs> SampleCtrlAdded;
        public event EventHandler<SampleControlEventArgs> SampleCtrlRemoved;

        public void AddSampleCtrl()
        {
            SsC_SampleCtrl ssc = new SsC_SampleCtrl();
            this.samples.Add(ssc);
            if (SampleCtrlAdded != null)
                SampleCtrlAdded(this, new SampleControlEventArgs(ssc));
        }

        private void listView1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode == Keys.Delete && listViewStructure.SelectedItems.Count > 0)
            {
                foreach (ListViewItem lvi in listViewStructure.SelectedItems)
                {
                    part.Material.Remove(lvi.Tag as Term);
                    listViewStructure.Items.Remove(lvi);
                }


            }
        }

        void comboBoxTerm_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            //if (e.KeyCode == Keys.Enter)
            //{
            //    OntologyHandlerResource ohr = comboBoxOntologyAlias.SelectedValue as OntologyHandlerResource;
            //    IMaterialSupervisor sup = comboBoxTerm.SelectedValue as IMaterialSupervisor;
            //    if((ohr.Handler as IOrganismStructureOntologyHandler).Supervisors.
            //    if (sup != null)
            //    {
            //        if (!part.Material.Contains(sup.TermId))
            //        {
            //            Material m = sup.EmitDefaultObject(true);
            //            part.Material.Add(m);
            //            ListViewItem lvi = new ListViewItem(m.Term);
            //            lvi.Tag = m;
            //            lvi.ToolTipText = sup.Definition;

            //            listView1.Items.Add(lvi);
            //        }
            //    }
            //}
        }

        private void kryptonButton1_Click(object sender, EventArgs e)
        {
            AddTermToList();
         }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            RemoveTerm();
        }

        private void RemoveTerm()
        {
            foreach (ListViewItem lvi in listViewStructure.SelectedItems)
            {
                part.Material.Remove(lvi.Tag as Term);
                listViewStructure.Items.Remove(lvi);
            }
        }

        private void RemovePosTerm()
        {
            foreach (ListViewItem lvi in listViewPositions.SelectedItems)
            {
                part.Positions.Remove(lvi.Tag as Term);
                listViewPositions.Items.Remove(lvi);
            }
        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {
            deleteToolStripMenuItem.Enabled = listViewStructure.SelectedItems.Count > 0;
        }

        private void contextMenuStrip2_Opening(object sender, CancelEventArgs e)
        {
            toolStripMenuItemDeletePosTerm.Enabled = listViewPositions.SelectedItems.Count > 0;
            editToolStripMenuItem.Enabled = listViewPositions.SelectedItems.Count > 0;
        }

        private void kryptonButtonAddPosTerm_Click(object sender, EventArgs e)
        {
            AddPosTermToList();
        }

        private void listViewPositions_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            EditPosTerm();
        }

        private void EditPosTerm()
        {
            if (listViewPositions.SelectedItems.Count > 0)
            {
                VariableTerm m = listViewPositions.SelectedItems[0].Tag as VariableTerm;
                IOntologyTerm sup = _currentPosTerms.Find(delegate(IOntologyTerm x) { return x.Prototype.Name.ToLower().Trim() == m.Name.ToLower().Trim(); });
                VariableTermEditFrm editFrm = new VariableTermEditFrm();
                editFrm.Setup(m, sup as IVariableOntologyTerm);
                editFrm.ShowDialog(this);
            }
        }

        private void editToolStripMenuItem_Click(object sender, EventArgs e)
        {
            EditPosTerm();
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            RemovePosTerm();
        }

        private void kryptonButtonShowStructHirachy_Click(object sender, EventArgs e)
        {
            OntologyHandlerResource ohr = comboBoxOntologyAlias.SelectedValue as OntologyHandlerResource;
            HirachicalViewFrm hvf = new HirachicalViewFrm();
            hvf.Setup(ohr.Handler);
            if (hvf.ShowDialog() == DialogResult.OK && hvf.SelectedTerm != null)
            {
                textBoxStructTerm.Text = hvf.SelectedTerm.Prototype.Name;
            }
        }

        private void kryptonButtonPosView_Click(object sender, EventArgs e)
        {
            OntologyHandlerResource ohr = comboBoxPosAlias.SelectedValue as OntologyHandlerResource;
            HirachicalViewFrm hvf = new HirachicalViewFrm();
            hvf.Setup(ohr.Handler);
            if (hvf.ShowDialog() == DialogResult.OK && hvf.SelectedTerm != null)
            {
                textBoxPosTerm.Text = hvf.SelectedTerm.Prototype.Name;
            }
        }

       
    }



    public class SampleControlEventArgs : EventArgs
    {
        private SsC_SampleCtrl control;
        public SsC_SampleCtrl Control
        {
            get { return control; }
        }

        public SampleControlEventArgs(SsC_SampleCtrl c)
        {
            this.control = c;
        }

    }
}
